Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business
Imports System.Web.UI.WebControls

Public Class SupplierList
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Masterpage1 As MyFramework.Web.UI.WebControls.MasterPage
    Protected WithEvents rptList As MyFramework.Web.UI.WebControls.Repeater
    Protected WithEvents pageTitle As MyFramework.Web.UI.WebControls.ContentRegion    
    Protected WithEvents crContent As MyFramework.Web.UI.WebControls.ContentRegion
    Protected WithEvents SortSupplierCodeLink As System.Web.UI.WebControls.LinkButton
    Protected WithEvents SortSupplierNameLink As System.Web.UI.WebControls.LinkButton
    Protected WithEvents lblSorting As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents ucPager As Pager
    Protected objConPoint As ConPoint = Nothing

    Private Sub LoadConPoint(ByVal conPointID As Integer)
        If conPointID > -1 Then
            If (objConPoint Is Nothing) OrElse objConPoint.ConPointID <> conPointID Then
                objConPoint = ConPointService.GetConPoint(conPointID)
            End If
        End If
    End Sub
    Protected Function GetConPoint(ByVal container As Object) As String
        Dim conPointID As Integer = -1
        Try
            conPointID = Convert.ToInt32(DataBinder.Eval(container, "DataItem.ConPointID"))
        Catch ex As Exception

        End Try
        LoadConPoint(conPointID)

        If (Not (objConPoint Is Nothing)) Then
            Return objConPoint.Description
        Else
            Return ""
        End If
    End Function

    Protected Function GetLoadingPort(ByVal container As Object) As String
        Dim conPointID As Integer = -1
        Try
            conPointID = Convert.ToInt32(DataBinder.Eval(container, "DataItem.ConPointID"))
        Catch ex As Exception

        End Try
        LoadConPoint(conPointID)

        If (Not (objConPoint Is Nothing)) Then
            Return objConPoint.LoadingPort
        Else
            Return ""
        End If

    End Function

    Private Property CurrentSortColumn() As String
        Get
            If (Session("CurrentSortColumn") Is Nothing) Then
                Session("CurrentSortColumn") = lblSorting.Text
            End If
            Return CType(Session("CurrentSortColumn"), String)
        End Get
        Set(ByVal Value As String)
            Session("CurrentSortColumn") = Value
        End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not IsPostBack Then
            If HttpContext.Current.Request.QueryString.Count = 0 Then ' User enters this page first time
                CurrentSortColumn = "Supplier Code"
            End If
            Dim sortColumn As SupplierColumns
            If (CurrentSortColumn = "Supplier Code") Then
                sortColumn = SupplierColumns.SupplierCode
            Else
                sortColumn = SupplierColumns.SupplierName
                lblSorting.Text = "Supplier Name"
            End If
            CurrentSortColumn = lblSorting.Text
            LoadGrid(sortColumn)
        End If
    End Sub

    Protected Sub SortLink_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim sortColumn As SupplierColumns
        If (DirectCast(sender, LinkButton).ClientID = "SortSupplierNameLink") Then
            sortColumn = SupplierColumns.SupplierName
            lblSorting.Text = "Supplier Name"
        Else
            sortColumn = SupplierColumns.SupplierCode
            lblSorting.Text = "Supplier Code"
        End If
        CurrentSortColumn = lblSorting.Text
        LoadGrid(sortColumn)
    End Sub

    Private Sub LoadGrid(ByVal sortColumn As SupplierColumns)
        Dim totalRecords As Integer
        ucPager.PageSize = Config.PageSize
        rptList.DataSource = SupplierService.GetSuppliers(sortColumn, _
            MyFramework.OrderDirection.ASC, ucPager.PageIndex + 1, ucPager.PageSize, totalRecords)
        rptList.DataBind()
        ucPager.TotalRecords = totalRecords
    End Sub

End Class
